var app = angular.module('meetingApp.controllers');

app.controller('articleCtr', ['$rootScope', '$scope', 'ajaxService', 'layerService', 'toastPCService', '$timeout', 'tempDataService', 'session',
    function ($rootScope, $scope, ajax, layer, toast, timeout, temp, session) {
        var edit = function () {
            return {id: '', title: '', content: '', status: '2'};
        };

        $scope.data = {
            list: [],
            edit: edit(),
            pages: 1,
            count: 0,
            search: {
                page: 1,
                pageSize: 15, title: ''
            },
            editInit:false
        };

        $scope.getList = function (page) {
            if (page) {
                $scope.data.search.page = page;
            }
            ajax('articleIndex', $scope.data.search, function (rel) {
                if (rel.errCode) {
                    toast.show(2, rel.message);
                } else {
                    $scope.data.list = rel.data.data;
                    $scope.data.pages = rel.data.last_page;
                    $scope.data.count = rel.data.total;
                }
            })
        }


        $scope.edit = function (item) {
            $scope.data.editInit = false
            if (item) {
                $scope.data.edit = angular.copy(item);
            } else {
                $scope.data.edit = edit();
            }
            setTimeout(()=>{
                $scope.$apply(function (){
                    $scope.data.editInit = true
                })
            })
            $('#myModal').modal();
        }

        $scope.commit = function () {
            if ($('#editForm').valid()) {
                ajax('articleSave', $scope.data.edit, function (rel) {
                    if (!rel.errCode) {
                        toast.show(1, '保存成功');
                        $scope.getList();
                        $('#myModal').modal('hide');
                    }
                })
            }
        }

        $scope.del = function (item) {
            layer.confirm('确定删除?', ['确定', '取消'], function () {
                ajax('articleDel', {id: item.id}, function (rel) {
                    if (!rel.errCode) {
                        toast.show(1, '删除成功');
                        $scope.getList();
                    }
                })
            })
        }


    }
])